package club.caihappy.server.service.impl;

import club.caihappy.server.mapper.MenuRoleMapper;
import club.caihappy.server.pojo.MenuRole;
import club.caihappy.server.pojo.RespBean;
import club.caihappy.server.service.IMenuRoleService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author seven
 * @since 2022-05-07
 */
@Service
public class MenuRoleServiceImpl extends ServiceImpl<MenuRoleMapper, MenuRole> implements IMenuRoleService {

    @Autowired
    private MenuRoleMapper menuRoleMapper;

    /**
     * 更新角色菜单
     *
     * @param rid
     * @param mids
     * @return
     */
    @Transactional
    @Override
    public RespBean updateMenuRoles(Integer rid, Integer[] mids) {
        int i = menuRoleMapper.delete(new QueryWrapper<MenuRole>().eq("rid", rid));
        if (mids == null || 0 == mids.length) {
            return RespBean.success("更新成功");
        }
        return menuRoleMapper.insertRecord(rid, mids) == mids.length ?
                RespBean.success("更新成功") : RespBean.success("更新失败");
    }
}
